<script setup>
import { ref } from 'vue'
import { use } from 'echarts/core'
import VChart from 'vue-echarts'
import { BarChart, PieChart } from 'echarts/charts'
import { TooltipComponent, LegendComponent, TitleComponent, GridComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
import { onMounted } from 'vue'
import request from '@/utils/http'

use([BarChart, PieChart, TooltipComponent, LegendComponent, TitleComponent, GridComponent, CanvasRenderer])

// 项目状态环形饼图
const Option = ref({
    tooltip: {
        trigger: 'item'
    },
    legend: {
        bottom: '0'
    },
    series: [
        {
            name: '项目状态',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            data: [
                { value: 45.45, name: '正常' },
                { value: 6.06, name: '关注' },
                { value: 10.30, name: '诉讼' },
                { value: 30.30, name: '执行终本' },
                { value: 7.88, name: '暂停' },
            ],
            itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
        }
    ]
})
onMounted(() => {
    changePie()
})
// 映射关系
const statusMap = {
  0: { name: '正常' },
  1: { name: '关注' },
  2: { name: '诉讼' },
  3: { name: '执行终本' },
  4: { name: '暂停' }
}
// 渲染饼状图
async function changePie(){
    try {
    const response = await request.get('/invest/projectInfo/get/status')
    console.log('接口返回数据',response);
    if(response.code === '0'){
        const processData = response.data.map(item => ({
          name:statusMap[item.status]?.name  ,
          value:item.count
        }))
        // 顺序固定
        .sort((a,b) => {
          const a1 = Object.keys(statusMap).find(key => statusMap[key].name === a.name)
          const b1 = Object.keys(statusMap).find(key => statusMap[key].name === b.name)
          return a1 - b1
        })
        // 替换整个数组
        Option.value.series[0].data = processData
}else{
    console.log('接口返回错误');
}
}catch (error) {
    console.dir(error);
}
}
</script>

<template>
    <div class="pieboard" style="height: 435px; width: 400px;">
        <p><span>|</span>项目状态</p>
        <v-chart :option="Option" style="height: 300px; width: 400px;" />
    </div>
</template>

<style scoped>
.pieboard {
    align-items: center;
    background-color: #ffffff;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.1);
    border: 1px solid #e5e5e5;
}

.pieboard p {
    font-size: 18px;
    color: #347CAF;
    margin-left: 15px;
    margin-top: 15px;
    span {
        font-weight: bold;
        font-size: 25px;
    }
}
</style>
